/**
 * 
 */
package org.sevaapp.service;

/**
 * @author spenchik
 *
 */
import java.util.HashMap;
import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.app.VelocityEngine;
import org.sevaapp.domain.Referral;
import org.sevaapp.security.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
import org.springframework.ui.velocity.VelocityEngineUtils;

@Service
@Scope("prototype")
public class ReferralServiceImpl extends BaseService implements ReferralService {
	
	private static final Log log = LogFactory.getLog(ReferralServiceImpl.class);

	@Autowired
	private Referral domainObj;

	@Autowired
	private JavaMailSender mailSender;

	@Autowired
	private VelocityEngine velocityEngine;

	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}

	public void setVelocityEngine(VelocityEngine velocityEngine) {
		this.velocityEngine = velocityEngine;
	}
   
	public boolean validate(Referral inputData) {
		return true;
	}

	public void addNewReferral(Referral inputData) {
		log.debug("addNewReferral() method called.");
		
		// Call validate method
		boolean isValidData = validate(inputData);
		log.debug("isValidData=" + isValidData);
		if (isValidData) {
			if (inputData.getId() == null) {
				// New record, call add method
				domainObj.add(inputData);
			} else {
				// Existing record, call edit method
				domainObj.update(inputData);
			}
		}
	   
//		// Send E-mail Confirmation
//		User user = new User();
//		user.setUserFullName(inputData.getApplicant().getFirstName()+ " " + inputData.getApplicant().getLastName());
//	
//		//String emailAddress = "lingamarlas@yahoo.com";
//		String emailAddress = inputData.getApplicant().getEmail();
//		user.setEmailAddress(emailAddress);
//		user.setReferralId(inputData.getId());
	
//		sendConfirmationEmail(user);
		
		// Send E-mail Confirmation
//		Referral refData = new Referral();
		sendConfirmationEmail(inputData);
	}

	public void updateReferral(Referral inputData) {
		log.debug("updateReferral() method called.");
		
		// Call validate method
		boolean isValidData = validate(inputData);
		log.debug("isValidData=" + isValidData);
		if (isValidData) {
			// Existing record, call edit method
			domainObj.update(inputData);
		}
	}

	public void register(User user) {
		// Do the registration logic...

//		sendConfirmationEmail(user);
	}

//	private void sendConfirmationEmail(final User user) {
	private void sendConfirmationEmail(final Referral refData) {
		MimeMessagePreparator preparator = new MimeMessagePreparator() {
			public void prepare(MimeMessage mimeMessage) throws Exception {
				MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
//				message.setTo(user.getEmailAddress());
//				message.setFrom("admin@miseva.org"); // could be parameterized...
//				message.setSubject("SEVA referral confirmation mail");
//				Map model = new HashMap();
//				model.put("user", user);
//				String text = VelocityEngineUtils.mergeTemplateIntoString(
//						velocityEngine, "registration-confirmation.vm", model);
//				message.setText(text, true);
				message.setTo(refData.getApplicant().getEmail());
				message.setCc("seva.hotline@gmail.com");
				message.setFrom("admin@miseva.org"); // could be parameterized...
				message.setSubject("SEVA referral confirmation mail - " + refData.getApplicant().getFirstName()+ " " + refData.getApplicant().getLastName());
				Map model = new HashMap();
				model.put("referral", refData);
				String text = VelocityEngineUtils.mergeTemplateIntoString(
						velocityEngine, "registration-confirmation.vm", model);
				message.setText(text, true);
			}
		};

		this.mailSender.send(preparator);
	}
}

